Skip to content

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Sep 13, 2025

  • It seems worth spelling out that this guarantee allows particular transmutes.
  • After the Result section was written, the Option section it referenced gained more guarantees, saying that None is represented as [0u8; N]. Those guarantees were not meant to apply to Result. Make the Result section more self-contained to make this more clear.
  • "Type has no fields" is unclear since there is no general definition of what the fields of some arbitrary type are. Replace that by a more accurate description of the actual check implemented here.

r? @traviscross

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 13, 2025
@RalfJung RalfJung force-pushed the res-opt-layout-guarantees branch from fe2ac5c to 0b3d10d Compare September 13, 2025 15:29
@RalfJung RalfJung force-pushed the res-opt-layout-guarantees branch from 0b3d10d to 57a8745 Compare September 13, 2025 15:45
@RalfJung RalfJung added the I-lang-nominated Nominated for discussion during a lang team meeting. label Sep 13, 2025
@traviscross traviscross added T-lang Relevant to the language team P-lang-drag-2 Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. and removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-2 Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang. labels Sep 13, 2025
@RalfJung
Copy link
Member Author

@traviscross was it okay to assign you as reviewer here? This isn't meant to say anything new, just to clarify that when we say

[Option<T>] has the same size, alignment, and [function call ABI] as T

then you can transmute between these two types, and that will preserve the values.

@theemathas
Copy link
Contributor

Does this interact with #147185?

@RalfJung
Copy link
Member Author

RalfJung commented Oct 17, 2025

I don't see how?

Result<NonZeroI32, ReprCZst> is indeed ABI-compatible with NonZeroI32 if ReprCZst truly is a ZST; we can do that since the ABI for this Result instance doesn't have to have anything to do with that for ReprCZst. Note that we already don't worry about the ZST being non-exhaustive or so: whether this type actually always is a ZST is your problem as unsafe code author relying on this guarantee.

This is quite different from repr(transparent) where the compiler checks whether the conditions are met, and where #[repr(transparent)] struct S((), ReprCZst) can be interpreted as being compatible with either type if we aren't careful.

RalfJung and others added 4 commits October 17, 2025 23:18
The notation used here (e.g. "transmute `t: T` to `Option<T>`") felt
maybe a bit heavy, in the context of the library documentation, so
let's elaborate this a bit.

Also, in the `Option` docs, we talk about this being true for "the
following types `T`", but it felt this caveat might get a bit lost in
the next sentence that talks about the valid transmutations, so let's
reiterate the caveat.

While we're touching the line, we can improve:

> The only difference is the implied semantics:

This sentence was a bit awkward due to the mismatched plurality and
not identifying the difference being spoken to.  We'll reword this to
make it more clear.

We'll wrap to 80, since the existing text and most of the doc comments
in these files are wrapped this way.
@traviscross traviscross force-pushed the res-opt-layout-guarantees branch from 30d3264 to 73f5fe7 Compare October 18, 2025 01:03
@rustbot
Copy link
Collaborator

rustbot commented Oct 18, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment on lines 122 to 124
//! [`Option<T>`] has the same size, alignment, and [function call ABI] as `T`.
//! It is therefore sound to transmute `t: T` to `Option<T>` (which will produce `Some(t)`), and
//! to transmute `Some(t): Option<T>` to `T` (which will produce `t`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the same way as what we ended up revising on #140463, probably I think it's more clear to elaborate the description a bit rather than leaning too heavily on this t: T notation where it's implied that t is a value. Not that it'd be entirely foreign to Rust programmers, maybe, but it still seems more clear to just spell it out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've revised the text here and in result.rs to elaborate this a bit.

Comment on lines 123 to 124
//! It is therefore sound to transmute `t: T` to `Option<T>` (which will produce `Some(t)`), and
//! to transmute `Some(t): Option<T>` to `T` (which will produce `t`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first sentence has the important caveat about this being true for "the following types T". In the second sentence, this caveat feels like it might get a bit lost to me, so it seems better to reiterate that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've revised to do this.

@traviscross
Copy link
Contributor

traviscross commented Oct 18, 2025

@RalfJung: Thanks for the ping. This had fallen off of my radar. I've pushed some revisions (see the commit message and the review comments for details) and rebased. Let me know that the revisions look OK to you, then r=me.

@RalfJung
Copy link
Member Author

The commit you added looks good to me, thanks!

@bors r=traviscross rollup

@bors
Copy link
Collaborator

bors commented Oct 18, 2025

📌 Commit 73f5fe7 has been approved by traviscross

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 18, 2025
bors added a commit that referenced this pull request Oct 18, 2025
Rollup of 6 pull requests

Successful merges:

 - #146509 (Result/Option layout guarantee clarifications)
 - #147494 (std::thread spawn: Docs: Link to Builder::spawn; Make same.)
 - #147532 ( Port `#[cfg_attr]` to the new attribute parsing infrastructure)
 - #147783 (bootstrap: migrate to object 0.37)
 - #147792 (Fix autodiff incorrectly applying fat-lto to proc-macro crates )
 - #147809 (rustdoc: Fix passes order so intra-doc links are collected after stripping passes)

Failed merges:

 - #147813 (Warn on unused_attributes in uitests )

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 32d21cb into rust-lang:master Oct 18, 2025
11 checks passed
@rustbot rustbot added this to the 1.92.0 milestone Oct 18, 2025
rust-timer added a commit that referenced this pull request Oct 18, 2025
Rollup merge of #146509 - RalfJung:res-opt-layout-guarantees, r=traviscross

Result/Option layout guarantee clarifications

- It seems worth spelling out that this guarantee allows particular transmutes.
- After the `Result` section was written, the `Option` section it referenced gained *more* guarantees, saying that `None` is represented as `[0u8; N]`. Those guarantees were not meant to apply to `Result`. Make the `Result` section more self-contained to make this more clear.
- "Type has no fields" is unclear since there is no general definition of what the fields of some arbitrary type are. Replace that by a more accurate description of the actual check implemented [here](https://github.com/rust-lang/rust/blob/e379c7758667f900aaf5551c4553c7d4c121e3e1/compiler/rustc_lint/src/types.rs#L828-L838).

r? `@traviscross`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-radar Items that are on lang's radar and will need eventual work or consideration. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants